<template>
	<view class="content">
		<view class="tips">上传本人真实的个人正脸照</view>
		<view class="avatar-wrap">
			<image class="avatar" :src="avatar" mode="aspectFill" @click="previewImage"></image>
		</view>
		<view class="btn-wrap">
			<button class="album" type="primary" @click="upload('album')">本地上传</button>
			<button class="camera" type="default" @click="upload('camera')">拍照上传</button>
		</view>
	</view>
</template>

<script>
	import qiniuUploader from '@/common/qiniu/qiniu_wx.js';
	export default {
		data() {
			return {
				userInfo: {},
				form: {
					file: ''
				},
				avatar: '',
				qiniuToken: '',
				domain: '',
				progress: 0,
			}
		},
		onLoad(options) {
			this.userInfo = JSON.parse(options.userinfo);
			this.avatar = this.userInfo.avatar;
		},
		onReady() {
			this.getQiniuToken();
		},
		onNavigationBarButtonTap(e) {
			if (this.form.file == '') {
				uni.showToast({
					title: '请上传头像',
					position: 'bottom',
					icon: 'none'
				});
				return false;
			}
			uni.showLoading({
				title: '',
			});
			this.$api.api.ajaxData({
				method: 'V1.UserUpload',
				uid: this.userInfo.id,
				token: this.userInfo.token,
				type: '3',
				file: this.form.file
			}, res => {
				uni.hideLoading();
				uni.showToast({
					title: res.msg,
					position: 'bottom',
					icon: 'none'
				});
				if (res.status) {
					// this.$tim.updateMyProfile({
					// 	avatar: this.QINIUURL + this.form.file,
					// }).then(imResponse => {
					// 	console.log('更新资料成功:', imResponse.data);
					// }).catch(imError => {
					// 	console.warn('更新资料失败:', imError);
					// });
					uni.$emit('my-page', {
						type: 'update',
					})
					uni.navigateBack({
						delta: 1
					})
				}
			}, err => {
				uni.hideLoading();
				uni.showToast({
					title: '网络错误，请稍后再试',
					position: 'bottom',
					icon: 'none'
				});
			})
		},
		methods: {
			getQiniuToken: function () {
				this.$api.api.ajaxData({
					method: 'V1.QiniuToken',
					uid: this.userInfo.id,
					token: this.userInfo.token,
				}, res => {
					if (res.status) {
						this.qiniuToken = res.data.token
						this.domain = res.data.domain
					}
				})
			},

			upload: function (type) {
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'],
					sourceType: [type],
					success: res => {
						let tempPaths = res.tempFilePaths[0].split('/');
						uni.showLoading({
							title: this.progress + '%'
						});
						qiniuUploader.upload(res.tempFilePaths[0], qiniu => {
							uni.hideLoading();
							this.avatar = res.tempFilePaths[0];
							this.form.file = qiniu.key;
						}, (err) => {
							console.log('err: ' + JSON.stringify(err));
						}, {
							region: 'SCN',
							domain: this.domain,
							key: tempPaths[tempPaths.length - 1],
							uptoken: this.qiniuToken,
						}, (res) => {
							this.progress = res.progress;
						}, () => {
							// 取消上传
						}, () => {
							// `before` 上传前执行的操作
						}, () => {
							// `complete` 上传接受后执行的操作(无论成功还是失败都执行)
						});
					}
				});
			},

			previewImage: function () {
				if (this.avatar != '') {
					uni.previewImage({
						current: 0,
						urls: [this.avatar],
						indicator: 'none',
						loop: false,
					});
				}
			},
		}
	}
</script>

<style lang="scss">
	@import '../../common/common.scss';
	.tips {
		font-size: 28rpx;
		color: $title-color;
		text-align: center;
		margin: 50rpx;
	}

	.avatar-wrap {
		display: flex;
		justify-content: center;
		align-items: center;

		.avatar {
			width: 480rpx;
			height: 480rpx;
			border-radius: 10rpx;
			background-color: $image-bg-color;
		}
	}

	.btn-wrap {
		margin: 50rpx;
		padding: 0 50rpx;

		button {
			border-radius: 100rpx;

			& + button {
				margin-top: 40rpx;
			}

			&.camera {
				border: 1rpx solid $main-color;
				color: $main-color;
			}
		}
	}
</style>
